package news.network;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import inter.Delegate;
import news.dto.NotifyMessages;

/**
 * 获取消息详细内容
 * Created by Administrator on 2016/3/23.
 */
public class GetDetailNew {
    Delegate delegate;
    StringRequest stringRequest;
    String new_id;

    public void setNew_id(String new_id) {
        this.new_id = new_id;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void get() {
        String ip = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + ip + "/Notify/GetDetailNotify";
        stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("获取新闻", response);
                if (delegate != null)
                    delegate.onResponse(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map map = new HashMap();
                map.put("notify_ID", new_id);
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public void cancel()
    {
        if (!stringRequest.isCanceled())
            stringRequest.cancel();
    }

    public static class ResponseDetailNotify {
        private String status;
        private NotifyMessages message;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public NotifyMessages getMessage() {
            return message;
        }

        public void setMessage(NotifyMessages message) {
            this.message = message;
        }
    }

}
